﻿using System;
using System.Collections;
using System.Data;
using Lib.Data;
using Lib.Utils;

namespace MS.INV.SysLst
{
    public class L_DVT : Lib.Bases.BaseList
    {
        public override string Name
        {
            get
            {
                return "L_DVT";
            }
        }

        public override string Captions
        {
            get { return "IdDVT,TenDVT#,Tên ĐVT";}
        }

        #region  Constructors
        public L_DVT()
        {
            m_DataLayer = new Lib.Data.DataLayer();
        }
        #endregion

        public override System.Data.DataTable LoadData()
        {
            return m_DataLayer.ExecDataTable("Lst_Sp_L_DVT", "Command", "LoadData");
        }

        public string GetNameByID(int v_IdDVT)
        {
            return Cvert.ToString(m_DataLayer.ExecScalar("Lst_Sp_L_DVT", "Command,IdDVT", "GetNameByID", v_IdDVT), "");
        }

        public override void OnUpdate(ref System.Data.DataTable Data)
        {
            Hashtable output = null;
            string s_ParamName = "";
            object[] o_ParamValue = null;

            try
            {
                m_DataLayer.BeginTransaction();
                foreach (DataRow row in Data.Rows)
                {
                    ParamHolder.ClearAll();
                    ParamHolder.AddParam("Command", "Update");
                    ParamHolder.AddParam("IdDVT", Cvert.ToInt32(row["IdDVT"], 0));
                    ParamHolder.AddParam("TenDVT", Cvert.ToString(row["TenDVT"], ""));
                    ParamHolder.GetParams(ref s_ParamName, ref o_ParamValue);
                    m_DataLayer.ExecNonQuery("Lst_Sp_L_DVT", s_ParamName, ref output, o_ParamValue);
                    if (!object.Equals(null, output))
                        row["IdDVT"] = Cvert.ToInt32(output["IdDVT"], 0);
                }
                m_DataLayer.CommitTransaction();
            }
            catch (Exception ex)
            {
                m_DataLayer.RollbackTransaction();
                throw ex;
            }
        }

        public override void OnDelete(DataRow[] Rows)
        {
            try
            {
                m_DataLayer.BeginTransaction();
                for (int i = 0; i < Rows.Length; i++)
                {
                    if (Cvert.ToBoolean(Rows[i]["Tick"], false))
                    {
                        m_DataLayer.ExecNonQuery("Lst_Sp_L_DVT", "Command,IdDVT", "Delete", Cvert.ToInt32(Rows[i]["IdDVT"], 0));
                    }
                }
                m_DataLayer.CommitTransaction();
            }
            catch (Exception ex)
            {
                m_DataLayer.RollbackTransaction();
                throw ex;
            }
        }
    }
}
